package com.xzssmk.leisure.x.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzssmk.leisure.x.mapper.ScenicSpotMapper;
import com.xzssmk.leisure.x.mapper.StoreLargessMapper;
import com.xzssmk.leisure.x.po.ScenicSpot;
import com.xzssmk.leisure.x.po.SelectModel;
import com.xzssmk.leisure.x.service.BaseService;
import com.xzssmk.leisure.x.service.ScenicSpotService;
import com.xzssmk.leisure.x.util.DataTables;
import com.xzssmk.leisure.x.util.RequestStatusUtil;

@Service
public class ScenicSpotServiceImpl extends BaseService implements ScenicSpotService {

    @Autowired
    private ScenicSpotMapper scenicSpotMapper;

    @Override
    public Map<String, Object> add(ScenicSpot scenicSpot) {

        scenicSpot.setCreateTime(new Date());
        scenicSpot.setModifyTime(new Date());
        Map<String, Object> map = new HashMap<>();
        Integer id = scenicSpotMapper.insert(scenicSpot);
        if (id > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> delete(List<Integer> idlist) {

        Map<String, Object> map = new HashMap<>();
        if (scenicSpotMapper.deleteByPrimaryKeys(idlist) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> update(ScenicSpot scenicSpot) {
        Map<String, Object> map = new HashMap<>();
        scenicSpot.setModifyTime(new Date());
        if (!StringUtils.isEmpty(scenicSpot.getLoginPassWord())) {
            scenicSpot.setLoginPassWord(DigestUtils.md5DigestAsHex(scenicSpot.getLoginPassWord().getBytes()));
        }

        //修改商户登录名的时候校验
        int count = scenicSpotMapper.getScenicSpotByLoginName(scenicSpot);
        if (count ==0) {//查名字
            if (scenicSpotMapper.updateByPrimaryKeySelective(scenicSpot) > 0) {
                map.put("status", RequestStatusUtil.SUCCESS);
            } else {
                map.put("status", RequestStatusUtil.FAILURE);
            }
        }

        if (count ==1) {//查对象id
            ScenicSpot scenicSpot1 = scenicSpotMapper.selectByLoginName(scenicSpot.getLoginName());
            if (scenicSpot.getScenicSpotId() == scenicSpot1.getScenicSpotId()) {
                if (scenicSpotMapper.updateByPrimaryKeySelective(scenicSpot) > 0) {
                    map.put("status", RequestStatusUtil.SUCCESS);
                } else {
                    map.put("status", RequestStatusUtil.FAILURE);
                }
            } else {
                map.put("status", RequestStatusUtil.FAILURE);
            }
        }

        return map;
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {

        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("modify_time desc");
        // https: // blog.csdn.net/lvbang_lzt/article/details/54375091
        // https: // blog.csdn.net/evane1890/article/details/63262610
        // https: // blog.csdn.net/qq_33315102/article/details/80572566
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(StoreLargessMapper.class, dataTables.getColumn());
            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        List<ScenicSpot> listScenicSpot = scenicSpotMapper.getPageList(dataTables.getSubSQL());
        PageInfo<ScenicSpot> pageInfo = new PageInfo<ScenicSpot>(listScenicSpot);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());

        return dataTables;
    }

    @Override
    public DataTables getUserPageList(DataTables dataTables) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("modify_time desc");
        // https: // blog.csdn.net/lvbang_lzt/article/details/54375091
        // https: // blog.csdn.net/evane1890/article/details/63262610
        // https: // blog.csdn.net/qq_33315102/article/details/80572566
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(StoreLargessMapper.class, dataTables.getColumn());
            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        List<ScenicSpot> listScenicSpot = scenicSpotMapper.getUserPageList(dataTables.getSubSQL());
        PageInfo<ScenicSpot> pageInfo = new PageInfo<ScenicSpot>(listScenicSpot);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());

        return dataTables;
    }

    @Override
    public ScenicSpot getById(Integer id) {

        return scenicSpotMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SelectModel> getSelectList() {
        return scenicSpotMapper.getSelectList();
    }

    @Override
    public ScenicSpot login(String loginname, String loginpassword) {
        return scenicSpotMapper.login(loginname, loginpassword);
    }
}
